package com.control;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.db.DBConnect;

public class LoginControl {
	/**
	 * 根据admin_num,admin_pwd判断是否是管理员
	 * @param admin_num
	 * @param admin_pwd
	 * @return
	 */
	public boolean isAdmin(String admin_num, String admin_pwd) {
		DBConnect dbc = new DBConnect();
		// String proc = "exec  selectAdmin "+user_id+","+user_pwd;
		String sql = "{call isAdmin('"+ admin_num +"', '"+ admin_pwd +"')}";	
		try {
			CallableStatement stmt = dbc.getConnection().prepareCall(sql); 
			ResultSet rs = stmt.executeQuery();
			if (rs.next())
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbc.close();
		}
		return false;
	}

	/**
	 * 根据stu_num,stu_pwd判断是否是学生
	 * @param stu_num
	 * @param stu_pwd
	 * @return
	 */
	public boolean isStu(String stu_num, String stu_pwd) {
		DBConnect dbc = new DBConnect();
		String sql="{call isStu('"+stu_num+"','"+stu_pwd+"')}";
		/*String sql = "SELECT * from student where stu_num ='" + stu_num
				+ "' and stu_pwd='" + stu_pwd + "'";
		*/
		try {
			CallableStatement stmt = dbc.getConnection().prepareCall(sql); 
			ResultSet rs = stmt.executeQuery();
			if (rs.next())
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbc.close();
		}
		return false;
	}
	
	/**
	 * 根据admin_num,admin_pwd获取admin_id
	 * @param admin_num
	 * @param admin_pwd
	 * @return
	 */
	public int getAdminId(String admin_num,String admin_pwd){
		DBConnect dbc = new DBConnect();
		String sql="{call getAdminId('"+admin_num+"','"+admin_pwd+"')}";
		/*String sql = "select admin_id from admin where admin_num = '"+ admin_num +"' " +
		*		"and admin_pwd = '"+ admin_pwd +"'";
		*/
		try {
			CallableStatement stmt = dbc.getConnection().prepareCall(sql); 
			ResultSet rs = stmt.executeQuery();
			if(rs.next())
			{
				return rs.getInt("admin_id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbc.close();
		}
		return 0;
	}
	
	/**
	 * 根据stu_num,stu_pwd获取stu_id
	 * @param stu_num
	 * @param stu_pwd
	 * @return
	 */
	public int getStuId(String stu_num,String stu_pwd){
		DBConnect dbc = new DBConnect();
		String sql="{call getStuId('"+stu_num+"','"+stu_pwd+"')}";
		/*String sql = "select stu_id from student where stu_num = '"+ stu_num +"' " +
		*		"and stu_pwd = '"+ stu_pwd +"'";
		*/
		try {
			CallableStatement stmt = dbc.getConnection().prepareCall(sql); 
			ResultSet rs = stmt.executeQuery();
			if(rs.next())
			{
				return rs.getInt("stu_id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbc.close();
		}
		return 0;
	}
}
